package communicationMessages;

import java.util.logging.Level;

import logger.LoggerWriter;
import data.MemberDataBase;

public class LoginMessage extends Message {

	@Override
	public String getAnswer() {
		String ans;
		String userName,password;
		LoggerWriter logger = LoggerWriter.getLoggerWriter(); 
		String arr[] = this.getContent().split("\n");
		if (arr.length == 2)
		{
			userName = arr[0];
			password = arr[1];
		}
		else
		{
			ans = "wrong format\n";
			logger.log("wrong format login msg", Level.WARNING);
			return ans;
		}
		MemberDataBase uDataBase = MemberDataBase.getMembersDataBase();
		if (uDataBase.authenticate(userName, password))
		{
			ans="login\nsuccess\n";
			logger.log("sucsses login " + userName,Level.INFO);
			uDataBase.addLoginMember(uDataBase.getMember(userName));
		}
		else
		{
			logger.log("failed user name password " + userName, Level.INFO);
			ans = "login\nfailed\nwrong user or password\n";
		}
		return ans;
	}

}
